package de.micromata.jak.demo;

import java.io.File;
import java.io.FileNotFoundException;

import de.micromata.opengis.kml.v_2_2_0.*;

/**
 * This example generates a KML file with a placemark and a chart for each continent. The chart is generated with the
 * Google Chart API and show the area (surface of the earth) of each continent.
 * 
 * Google Chart API example: http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World
 */
public class Example1 {

  static Document doc = null;

  public static void main(String[] args) throws FileNotFoundException {

    // generate charts with the statistic data (percental surface of the earth)
    // example of a chart URL:
    // http://chart.apis.google.com/chart?
    // chs=380x200&amp;chd=t:20,80&amp;cht=p3&amp;chl=Africa|remaining&amp;chtt=Earth's surface
    String title = "Earth's surface";
    String[] africaChart = Utils.generate3dPieChart(380, 200, new int[] { 20 }, new String[] { "Africa", "remaining" },
        title);
    String[] asiaChart = Utils.generate3dPieChart(380, 200, new int[] { 30 }, new String[] { "Asia", "remaining" },
        title);
    String[] australiaChart = Utils.generate3dPieChart(380, 200, new int[] { 6 }, new String[] { "Australia",
        "remaining" }, title);
    String[] europeChart = Utils.generate3dPieChart(380, 200, new int[] { 7 }, new String[] { "Europe", "remaining" },
        title);
    String[] n_americaChart = Utils.generate3dPieChart(430, 200, new int[] { 17 }, new String[] { "North America",
        "remaining" }, title);
    String[] s_americaChart = Utils.generate3dPieChart(430, 200, new int[] { 12 }, new String[] { "South America",
        "remaining" }, title);

    // create KML object and root element: doc
    final Kml kml = new Kml();
    doc = kml.createAndSetDocument().withName("JAK Example1").withOpen(true);

    // create placemarks
    Placemark africa = setPlacemark(19.44601806124206, 10.13133611111111, 11582113, "Africa", africaChart);
    Placemark asia = setPlacemark(93.24607775062842, 47.49808862281773, 13888166, "Asia", asiaChart);
    Placemark australia = setPlacemark(135.0555272486322, -26.23824399654937, 13978129, "Australia", australiaChart);
    Placemark europe = setPlacemark(14.45531426360271, 47.26208181151567, 6222897, "Europe", europeChart);
    Placemark n_america = setPlacemark(-103.5286299241638, 41.26035225962401, 13987748, "North America", n_americaChart);
    Placemark s_america = setPlacemark(-59.96161780270248, -13.27347674076888, 13988211, "South America",
        s_americaChart);

    // create a folder
    Folder folder = doc.createAndAddFolder();
    folder.withName("Continents with Earth's surface").withOpen(true);
    folder.addToFeature(africa);
    folder.addToFeature(asia);
    folder.addToFeature(australia);
    folder.addToFeature(europe);
    folder.addToFeature(n_america);
    folder.addToFeature(s_america);

    // print and save
    kml.marshal();
    kml.marshal(new File("src/main/resources/examples/example1.kml"));
  }

  /**
   * Generate and return a placemark object. The Icon and Style objects (color and size of the text and icon) are saved
   * to the root element. The placemark will be created and returned only.
   * 
   * @param longitude
   * @param latitude
   * @param range
   *          (distance between the point on the ground and the LookAt point)
   * @param continent
   *          or name of the placemark
   * @param chartURL
   *          array of URL strings. [0]: chart, [1]: icon/preview
   * @return placemark with the coordinates (longitude, latitude, 0)
   */
  private static Placemark setPlacemark(double longitude, double latitude, double range, String continent,
      String[] chartURL) {

    String description = "<![CDATA[<img src=\"" + chartURL[0] + "\" />"; // chart image URL
    Icon icon = new Icon();
    icon.withHref(chartURL[1]); // set chart icon
    Style style = doc.createAndAddStyle();
    style.withId("style_" + continent) // set the stylename to use this style from the placemark
        .createAndSetIconStyle().withScale(5.0).withIcon(icon); // set size and icon
    style.createAndSetLabelStyle().withColor("ff43b3ff").withScale(5.0); // set color and size of the continent name

    Placemark pm = new Placemark();
    // use the style for each continent
    pm.withName(continent).withStyleUrl("#style_" + continent)
    // 3D chart imgae
        .withDescription(description)
        // coordinates and distance (zoom level) of the viewer
        .createAndSetLookAt().withLongitude(longitude).withLatitude(latitude).withAltitude(0).withRange(range);

    pm.createAndSetPoint().addToCoordinates(longitude, latitude); // set coordinates
    return pm;
  }

}
